package com.nitramf.catchmystream.entities;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;

/**
 *
 * @author martinfilliau
 */
@Entity
@NamedQueries({
    @NamedQuery(name=Keyword.QUERY_FIND_BY_NAME, query="SELECT k FROM Keyword AS k WHERE k.name = :name")
})
@Indexed
public class Keyword extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Basic
    @Field(index=Index.TOKENIZED, store=Store.YES)
    private String name;

    @Basic
    private String uri;

    @ManyToMany(mappedBy = "keywords")
    private List<Item> items;


    /* Queries */

    /**
     * Find a keyword by its name
     * @param name name of the keyword to find
     */
    public static final String QUERY_FIND_BY_NAME = "Keyword.findByName";


    /* GETTERs and SETTERs */

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }
    

    /* Overriden methods */

    @Override
    public int hashCode() {
        int hash = 0;
        hash += ( id != null ? id.hashCode() : 0 );
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!( object instanceof Keyword )) {
            return false;
        }
        Keyword other = (Keyword) object;
        if (( this.id == null && other.id != null ) || ( this.id != null && !this.id.equals(other.id) )) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.nitramf.catchmystream.entities.Keyword[id=" + id + "]";
    }

}
